/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Buscar_Item.java
 *
 * Created on 2/03/2011, 02:11:06 PM
 */
package puntoventa;

import java.sql.*;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gustavo Diaz
 */
public class Buscar_Item extends javax.swing.JFrame {

    private final DefaultTableModel modelo;

    /** Creates new form Buscar_Item */
    public Buscar_Item() {
        initComponents();
        this.setLocationRelativeTo(this);
        modelo = (DefaultTableModel) this.mostrarInv.getModel();
        mostrarTabla();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BuscarB = new javax.swing.JButton();
        Title = new javax.swing.JLabel();
        tipoLabel = new javax.swing.JLabel();
        SubTittle = new javax.swing.JLabel();
        tipoField = new javax.swing.JTextField();
        tipoBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        BuscarB1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        mostrarInv = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BuscarB.setFont(new java.awt.Font("Tahoma", 1, 14));
        BuscarB.setText("Buscar");
        BuscarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarBActionPerformed(evt);
            }
        });

        Title.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        Title.setText("Consulta de Productos");

        tipoLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tipoLabel.setText("ID:");

        SubTittle.setText("Ingrese el criterio de busqueda para su producto:");

        tipoBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "Nombre", "Proveedor" }));
        tipoBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoBoxActionPerformed(evt);
            }
        });

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Tipo:");

        BuscarB1.setFont(new java.awt.Font("Tahoma", 0, 14));
        BuscarB1.setText("Cancelar");
        BuscarB1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarB1ActionPerformed(evt);
            }
        });

        mostrarInv.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nombre", "Proveedor", "Precio", "Stock"
            }
        ));
        mostrarInv.setEnabled(false);
        jScrollPane1.setViewportView(mostrarInv);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(SubTittle)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(tipoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(tipoBox, 0, 154, Short.MAX_VALUE)
                                            .addComponent(tipoField))))
                                .addGap(18, 18, 18)
                                .addComponent(BuscarB, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(Title, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BuscarB1, 0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(SubTittle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tipoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tipoLabel)
                            .addComponent(tipoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(BuscarB, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(BuscarB1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tipoBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoBoxActionPerformed
        String filtro = tipoBox.getSelectedItem().toString();

        if (filtro.equals("ID")) {
            tipoLabel.setText("ID:");

        } else if (filtro.equals("Nombre")) {

            tipoLabel.setText("Nombre:");

        } else if (filtro.equals("Proveedor")) {

            tipoLabel.setText("Proveedor:");
        }



    }//GEN-LAST:event_tipoBoxActionPerformed

    private void BuscarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarBActionPerformed
        DefaultTableModel mod = (DefaultTableModel) mostrarInv.getModel();  

        for (int x = mod.getRowCount()-1; x > 0; x--) {
            System.out.println("contador ="+x+"");
                mod.removeRow(x);
            }

            if (mod.getRowCount() == 1) {
                mod.removeRow(0);
            }
        try {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            Connection conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/PDV", "root", "");
            Statement s = (Statement) conexion.createStatement();
            java.sql.Statement st = conexion.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM  productos where " + tipoBox.getSelectedItem() + "='" + tipoField.getText() + "'");
//            DefaultTableModel mod = (DefaultTableModel) mostrarInv.getModel();



//            for (int x = 0; x < mod.getRowCount(); x++) {
//                System.out.println("Aqui el contador vale= "+x+"");
//                mod.removeRow(x);
//            }
//
//            if (mod.getRowCount() == 1) {
//                mod.removeRow(0);
//            }
//            System.out.println(mod.getRowCount() + "  asd");

            while (rs.next()) {

                String ID_q = rs.getString("ID");
                String nombre_q = rs.getString("nombre");
                String provee_q = rs.getString("proveedor");
                int precio_q = rs.getInt("precio");
                int stock_q = rs.getInt("stock");

                System.out.println(ID_q + nombre_q + provee_q + precio_q + stock_q);


                Object fortable[] = {ID_q, nombre_q, provee_q, precio_q, stock_q};

                mod.addRow(fortable);
            }
            System.out.println(mod.getRowCount());
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(Buscar_Item.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BuscarBActionPerformed

    private void BuscarB1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarB1ActionPerformed
        Administrador Admin = new Administrador();
        Admin.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_BuscarB1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Buscar_Item().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BuscarB;
    private javax.swing.JButton BuscarB1;
    private javax.swing.JLabel SubTittle;
    private javax.swing.JLabel Title;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable mostrarInv;
    private javax.swing.JComboBox tipoBox;
    private javax.swing.JTextField tipoField;
    private javax.swing.JLabel tipoLabel;
    // End of variables declaration//GEN-END:variables

    private void mostrarTabla() {
        try {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            Connection conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/PDV", "root", "");
            Statement s = (Statement) conexion.createStatement();
            ResultSet rs = s.executeQuery("select * from Productos");
            Object[] fila = new Object[5];
            while (rs.next()) {
                fila[0] = rs.getString("ID");
                fila[1] = rs.getString("Nombre");
                fila[2] = rs.getString("Proveedor");
                fila[3] = rs.getString("Precio");
                fila[4] = rs.getString("Stock");

                modelo.addRow(fila);
                this.mostrarInv.setModel(modelo);
            }
            conexion.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
